package br.elsa.survey.tests.utils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import br.elsa.survey.domain.questions.CheckboxOption;
import br.elsa.survey.domain.questions.CheckboxQuestion;
import br.elsa.survey.domain.questions.DiscursiveQuestion;
import br.elsa.survey.domain.questions.Level;
import br.elsa.survey.domain.questions.Question;
import br.elsa.survey.domain.questions.RadioOption;
import br.elsa.survey.domain.questions.RadioQuestion;

/**
 * Helper Class used to create instances of {@link Question}
 * 
 * @author diego
 * @since 2010-10-03
 */
public class QuestionsFactory {

	/**
	 * Sequence value used as suffix for {@link Question} text attribute.
	 */
	public static Integer questionsSequesnce = 1;

	/**
	 * Creates a {@link CheckboxQuestion} with number of options equals to
	 * {@code numberOfOptions} parameter.
	 * 
	 * <p>
	 * <ol>
	 * <li>
	 * The question text field is automatically created and it's value is
	 * composed by {@link Class#getSimpleName()} concatenated with
	 * {@link QuestionsFactory#questionsSequesnce} that computes the number of
	 * created questions.</li>
	 * <li>
	 * The option text field is automatically created and it's value is composed
	 * by {@link Class#getSimpleName()} concatenated with an integer value in
	 * the range 0 and {@code numberOfOptions} parameter.</li>
	 * </ol>
	 * </p>
	 * 
	 * @param numberOfOptions
	 *            number of {@link CheckboxOption} elements for the question.
	 * 
	 * @return An instance of {@link CheckboxQuestion} filled with
	 *         {@code numberOfOptions} {@link CheckboxOption} instances
	 */
	public static CheckboxQuestion createCheckboxQuestion(Integer numberOfOptions) {

		List<CheckboxOption> options = new ArrayList<CheckboxOption>();

		CheckboxQuestion question = new CheckboxQuestion();
		question.setText(CheckboxQuestion.class.getSimpleName() + questionsSequesnce);
		question.setCreationDate(new Date(System.currentTimeMillis()));
		question.setLevel(Level.Easy);
		question.setOptions(options);

		for (int i = 0; i < numberOfOptions; i++) {

			CheckboxOption option = new CheckboxOption();
			option.setText(CheckboxOption.class.getSimpleName() + i);
			option.setQuestion(question);
			options.add(option);
		}

		questionsSequesnce++;

		return question;
	}

	/**
	 * Creates a {@link RadioQuestion} with number of options equals to
	 * {@code numberOfOptions} parameter.
	 * 
	 * <p>
	 * <ol>
	 * <li>
	 * The question text field is automatically created and it's value is
	 * composed by {@link Class#getSimpleName()} concatenated with
	 * {@link QuestionsFactory#questionsSequesnce} that computes the number of
	 * created questions.</li>
	 * <li>
	 * The option text field is automatically created and it's value is composed
	 * by {@link Class#getSimpleName()} concatenated with an integer value in
	 * the range 0 and {@code numberOfOptions} parameter.</li>
	 * </ol>
	 * </p>
	 * 
	 * @param numberOfOptions
	 *            number of {@link RadioOption} elements for the question.
	 * 
	 * @return An instance of {@link RadioQuestion} filled with
	 *         {@code numberOfOptions} {@link RadioOption} instances
	 */
	public static RadioQuestion createRadioQuestion(Integer numberOfOptions) {

		List<RadioOption> options = new ArrayList<RadioOption>();

		RadioQuestion question = new RadioQuestion();
		question.setText(RadioQuestion.class.getSimpleName() + questionsSequesnce);
		question.setCreationDate(new Date(System.currentTimeMillis()));
		question.setLevel(Level.Easy);
		question.setOptions(options);

		for (int i = 0; i < numberOfOptions; i++) {

			RadioOption option = new RadioOption();
			option.setText(RadioOption.class.getSimpleName() + i);
			option.setQuestion(question);
			options.add(option);
		}

		questionsSequesnce++;

		return question;
	}

	public static DiscursiveQuestion createDiscursiveQuestion() {

		DiscursiveQuestion question = new DiscursiveQuestion();
		question.setText(Question.class.getSimpleName() + questionsSequesnce);
		question.setCreationDate(new Date(System.currentTimeMillis()));
		question.setLevel(Level.Easy);

		questionsSequesnce++;

		return question;
	}
}
